/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;

import Hibernate.HibernateUtil;
import pojo.Book;
import pojo.Link;
import pojo.Topic;
import pojo.Website;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Vinh Quang
 */
public class LinkDAO extends AbstractDAO{
	
	@Override
	protected Class getPOJOClass() {
		// TODO Auto-generated method stub
		return Link.class;
	}
    private static TopicDAO topicdao = null;
    private static WebsiteDAO websitedao = null;
    
    static{
        topicdao = new TopicDAO();
        websitedao = new WebsiteDAO();
    }
    
    public Link getWebsiteByID(int id){
        Link link = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            link = (Link)session.get(Link.class, id);
        }
        catch(HibernateException ex){
            
        }
        finally{
            session.close();
        }
        return link;
    }
    
    public void finish(int topicId, String keywords){
        Link link = new Link();
        link.setLinkUrl("UnknowLinkUrl");
        link.setKeywords(keywords);
        link.setTopic(topicdao.getTopicByID(topicId));
        link.setWebsite(websitedao.getWebsiteByID(1));
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try{
            transaction = session.beginTransaction();
            session.save(link);
            transaction.commit();;
        }
        catch(HibernateException ex){
            transaction.rollback();
        }
        finally{
            session.close();
        }
    }
    public void finish2(Link link, int topicID, int mark){
    	link.setTopic(topicdao.getTopicByID(topicID));
    	link.setMark(mark);
    	Session session = HibernateUtil.getSessionFactory().openSession();
    	Transaction transaction = null;
    	try{
    		transaction = session.beginTransaction();
    		session.saveOrUpdate(link);
    		transaction.commit();
    		System.out.println("a");
    	}catch(HibernateException ex){
    		transaction.rollback();
    	}finally{
    		session.close();
    	}
    }
    public boolean insert(int webId, int topicId, String strlink, String key)
    {
    	boolean flag = false;
    	WebsiteDAO webDAO= new WebsiteDAO();
    	Website web= webDAO.getWebsiteByID(webId);
    	
    	TopicDAO topicDAO= new TopicDAO();
    	Topic topic=null;
    	if(topicId!=0)
    	{
    		topic= topicDAO.getTopicByID(topicId);
    	}
    	
    	Link link = new Link(web,topic,strlink,key);
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.save(link);
			session.flush();
			transaction.commit();
			flag=true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
    	
    	return flag;
    }
    
    public boolean delete(Link link)
    {
    	boolean flag= false;
    	LinkDAO linkdao= new LinkDAO();
    	Link l= (Link) linkdao.getWebsiteByID(link.getLinkId());
    	if(l==null) return flag;
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		/*	
		try {
			transaction = session.beginTransaction();
			
			String sql = "select book from Book sp where book. sp.nhaSanXuat.idNhaSanXuat = :ID"
					;
			Query query = (Query) session.createQuery(sql).setString("ID", id);
			List<SanPham> lsp1= query.list();

			for(SanPham sp : lsp1)
			{
				
				sql = "select cm from Comment cm where cm.sanPham.idSanPham = :ID";
					
				 query = (Query) session.createQuery(sql).setString("ID",sp.getIdSanPham());
				List<Comment> lcm=query.list();
				
				for(Comment cm :lcm)
				{
					session.delete(cm);
				}
				session.delete(sp);
			}

			sql = "select s from NhaSanXuat s where s.idNhaSanXuat = :ID";
			 query = (Query) session.createQuery(sql)
					.setString("ID", id);
			List l = query.list();
			NhaSanXuat s=(NhaSanXuat)l.get(0);
			session.delete(s);
			session.flush();
			transaction.commit();
			flag = true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
		
	*/
		
		
		return flag;
    	
    }

	public Link getlink(String linkUrl) {
		// TODO Auto-generated method stub
		List<Link> l=this.getList();
		for(int i=0; i< l.size();i++)
		{
			if(l.get(i).getLinkUrl().equals(linkUrl))
				return l.get(i);
		}
		return null;
	}
	
	public List<Link> getlink(int id)
	{
		List<Link> ds= null;
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = "select s from Link s where s.linkId >=:n";
		Query query = session.createQuery(hql).setParameter("n", id);
		 ds = (List<Link>) query.list();
		session.close();
		return ds;
	}
	
}
